<?php
/**
 * 公共返回方法
 * Author: JiaMeng <666@majiameng.com>
 * Date: 2017/11/23
 * Time: 17:38
 */
namespace App\Services;

use App\Models\StatusCode;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\DB;
use JetBrains\PhpStorm\NoReturn;
use tinymeng\tools\FileTool;

class CommonReturn
{

    /** ----------------------------------------------------------------------------------------------- */

    /**
     * Description:  请求成功
     * Author: JiaMeng <666@majiameng.com>
     * @param array $data 返回数据
     * @param string $message 状态信息
     * @param bool $toArray
     * @return void
     */
    #[NoReturn] public static function success($data=[], $message='', $toArray=true): void
    {
        if($data instanceof JsonResource){
            /** 处理JsonResource */
            if($toArray == true){
                $data = [
                    'count'=>$data->total(),
                    'page'=>$data->currentPage(),
                    'limit'=>$data->perPage(),
                    'list'=>$data->toArray(request()),
                ];
            }else{
                $data = $data->toArray(request());
            }
        }
        $result = [
            'code' => StatusCode::COMMON_SUCCESS,//layer(0表示成功，其它表示失败) 200成功
            'msg' => empty($message) ? 'success' : $message,//成功/失败 信息
            'data' => is_bool($data)?array():$data,
        ];
        self::response($result);
    }

    /**
     * Description:  请求失败
     * Author: JiaMeng <666@majiameng.com>
     * @param int $status_code 状态码
     * @param string $message 状态信息
     * @param array $data 返回数据
     */
    #[NoReturn] static public function fail(int $status_code = StatusCode::COMMON_UNKNOWN , string $message = '' , array $data = []): void
    {
        DB::rollBack();
        if(empty($message)) $message = StatusCode::$status_code[$status_code] ?? StatusCode::$status_code[StatusCode::COMMON_UNKNOWN];
        switch ($status_code){
            case StatusCode::COMMON_SAVE_FAILURE:
                if(!env('APP_DEBUG')){
                    FileTool::writeLog($message.',url:'.url()->full().' ,post:'.json_encode($_POST).' ,get:'.json_encode($_GET),'fail_sql_save_error');//记录错误sql
                    $message = '服务器异常,请联系管理员!';
                }
                break;
            case StatusCode::COMMON_UNKNOWN:
                if(!env('APP_DEBUG')){
                    FileTool::writeLog($message.',url:'.url()->full().' ,post:'.json_encode($_POST).' ,get:'.json_encode($_GET),'fail_unknown_error');//记录未知
                    $message = '服务器异常,请联系管理员!';
                }
                break;
            default:
                FileTool::writeLog($message.',url:'.url()->full().' ,post:'.json_encode($_POST).' ,get:'.json_encode($_GET),'fail_error');//记录未知
                break;
        }

        $result = [
            'code' => $status_code,                                 //0表示成功，其它表示失败
            'msg' => empty($message) ? 'success' : $message,   //成功/失败 信息
        ];
        if(!empty($data)) $result['data'] = $data;
        self::response($result);
    }

    /** ----------------------------------------------------------------------------------------------- */

    /**
     * Description:  响应客户端
     * Author: JiaMeng <666@majiameng.com>
     * Updater:
     * @param $result
     * @param int $status
     * @return void
     */
    #[NoReturn] public static function response($result, int $status=200): void
    {
        /** 处理长int类型转为字符串 */
        CommonServices::longIntToStr($result);
        response($result,$status)->send();
        exit();
    }

}
